// $Id: DOMUtils.java 70 2011-12-08 15:12:12Z huijzer@gmail.com $
package lime.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class DOMUtils {

	private DOMUtils() {
		// Empty
	}

	public static Document newDocument() throws ParserConfigurationException {

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.newDocument();

		return doc;
	}

	public static void serialize(Node node, OutputStream outputStream) throws ClassCastException,
			ClassNotFoundException, InstantiationException, IllegalAccessException {

		DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
		DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");

		LSOutput output = impl.createLSOutput();
		output.setByteStream(outputStream);
		output.setEncoding("utf-8");

		LSSerializer serializer = impl.createLSSerializer();
		serializer.getDomConfig().setParameter("format-pretty-print", true);
		serializer.write(node, output);
	}

	public static String serialize(Node node) throws ClassCastException, ClassNotFoundException,
			InstantiationException, IllegalAccessException {

		DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
		DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");

		Writer writer = new StringWriter();
		LSOutput output = impl.createLSOutput();
		output.setCharacterStream(writer);
		output.setEncoding("utf-8");
		
		LSSerializer serializer = impl.createLSSerializer();
		serializer.getDomConfig().setParameter("format-pretty-print", true);
		serializer.write(node, output);

		return writer.toString();
	}

	public static Document parse(InputStream in) throws ParserConfigurationException, SAXException, IOException {

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.parse(in);

		return doc;
	}

	public static Document parse(String in) throws ParserConfigurationException, SAXException, IOException {

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		StringReader reader = new StringReader(in);
		InputSource source = new InputSource(reader);
		Document doc = builder.parse(source);

		return doc;
	}
	
}
